package MOLE;

//���������� �ṩ���ܺͽ��ܵķ���
//******************************************************************/

/** 
 ���ܺͽ���
 
*/
public final class EncryptDecrypt
{
	private EncryptDecrypt()
	{
	}


	public static String passkey = "ngaijen,ni arg lalei l a!";
	/** 
	 ����MD5���ַ������м���
	 
	 �����ܵ��ַ���
	 ���ؼ��ܺ���ַ���
	*/
	public static String EncryptMD5(String encryptString)
	{
		String s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encryptString,"md5");
		return s;
	}

	public static String MD5(String s)
	{
		  return EncryptMD5(s);
	}
	public static String TMD5(String s)
	{
			s = MD5(s);
			s = MD5(s);
			s = MD5(s);
			return s;
	}
	public static String MD5(String s, String _input_charset)
	{

		/** 
		 ��ASP���ݵ�MD5�����㷨
		 YASH alipay sheo yond swefa
		*/

		MD5 md5 = new MD5CryptoServiceProvider();
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
		byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
		// byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s));
		StringBuilder sb = new StringBuilder(32);
		for (int i = 0; i < t.length; i++)
		{
			sb.append(tangible.DotNetToJavaStringHelper.padLeft(String.format("%x", t[i]), 2, '0'));
		}
		return sb.toString();
	}

	/** 
	 DES�����ַ���
	 
	 �����ܵ��ַ���
	 ������Կ,Ҫ��Ϊ8λ
	 ���ܳɹ����ؼ��ܺ���ַ�����ʧ�ܷ���Դ��
	
	*/
	public static String EncryptDES(String str)
	{
		String s = passkey;
		return EncryptDES(str, s);
	}
	public static String EncryptDES(String encryptString, String encryptKey)
	{
		//Ĭ����Կ����
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
		   byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

			   //get long str,if str <8.
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbKey = Encoding.UTF8.GetBytes(MD5(encryptKey).Substring(0,8));
			byte[] rgbKey = Encoding.UTF8.GetBytes(MD5(encryptKey).substring(0,8));
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbIV = Keys;
			byte[] rgbIV = Keys;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
			byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
			DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
			MemoryStream mStream = new MemoryStream();
			CryptoStream cStream = new CryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
			cStream.Write(inputByteArray,0,inputByteArray.length);
			cStream.FlushFinalBlock();
			return Convert.ToBase64String(mStream.ToArray());

	}

	/** 
	 DES�����ַ���
	 
	 �����ܵ��ַ���
	 ������Կ,Ҫ��Ϊ8λ,�ͼ�����Կ��ͬ
	 ���ܳɹ����ؽ��ܺ���ַ�����ʧ�ܷ�Դ��
	
	*/
	public static String DecryptDES(String str)
	{
		String s = passkey;
		return DecryptDES(str, s);
	}
	public static String DecryptDES(String decryptString, String decryptKey)
	{
		try
		{
			//Ĭ����Կ����
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
			byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
			 //get long str,if str <8.
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbKey = Encoding.UTF8.GetBytes(MD5(decryptKey).Substring(0,8));
			byte[] rgbKey = Encoding.UTF8.GetBytes(MD5(decryptKey).substring(0,8));

//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbIV = Keys;
			 byte[] rgbIV = Keys;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] inputByteArray = Convert.FromBase64String(decryptString);
			byte[] inputByteArray = Convert.FromBase64String(decryptString);
			DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
			MemoryStream mStream = new MemoryStream();
			CryptoStream cStream = new CryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
			cStream.Write(inputByteArray,0,inputByteArray.length);
			cStream.FlushFinalBlock();
			return System.Web.HttpUtility.UrlDecode(Encoding.UTF8.GetString(mStream.ToArray()));
		}
		catch (java.lang.Exception e)
		{
			return "";
		}

	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#region  des file
	/** 
	 DES�����ļ�
	 
	 
	 �������ļ�
	 ���ܺ���ļ�
	 ������Կ
	 
	*/
	public static boolean EncryptDES(String inFilePath, String outFilePath, String encryptKey)
	{
		//Ĭ����Կ����
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
		byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbIV= Keys;
		byte[] rgbIV = Keys;
		try
		{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
			byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.substring(0,8));
			//�������
			FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
			//��д����
			FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
			outFs.SetLength(0);
			//����һ��������������д
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] byteIn = new byte[100];
			byte[] byteIn = new byte[100]; //��ʱ��Ŷ������
			long readLen = 0; //�������ĳ���
			long totalLen = inFs.getLength(); //�ܹ��������ĳ���
			int everyLen; //ÿ�ζ�����������
			//����InFs�����ܺ�д��OutFs
			DES des = new DESCryptoServiceProvider();
			CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
			while (readLen < totalLen)
			{
				everyLen = inFs.Read(byteIn, 0, 100);
				encStream.Write(byteIn, 0, everyLen);
				readLen = readLen + everyLen;
			}
			encStream.Close();
			outFs.Close();
			inFs.Close();
			return true; //���ܳɹ�
		}
		catch (java.lang.Exception e)
		{
			return false; //����ʧ��
		}
	}


	/** 
	 DES�����ļ�
	 
	 �������ļ�
	 �������ļ�
	 ������Կ
	 
	*/
	public static boolean DecryptDES(String inFilePath, String outFilePath, String decryptKey)
	{
		//Ĭ����Կ����
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
		byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbIV= Keys;
		byte[] rgbIV = Keys;
		try
		{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
			byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.substring(0,8));
			//�������
			FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
			//��д����
			FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
			outFs.SetLength(0);
			//����һ��������������д
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] byteIn = new byte[100];
			byte[] byteIn = new byte[100]; //��ʱ��Ŷ������
			long readLen = 0; //�������ĳ���
			long totalLen = inFs.getLength(); //�ܹ��������ĳ���
			int everyLen; //ÿ�ζ�����������
			//����InFs�����ܺ�д��OutFs
			DES des = new DESCryptoServiceProvider();
			CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
			while (readLen < totalLen)
			{
				everyLen = inFs.Read(byteIn, 0, 100);
				encStream.Write(byteIn, 0, everyLen);
				readLen = readLen + everyLen;
			}
			encStream.Close();
			outFs.Close();
			inFs.Close();
			return true; //���ܳɹ�
		}
		catch (java.lang.Exception e)
		{
			return false; //����ʧ��
		}
	}
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
		///#endregion



}